//
//  ANAlertManager.m
//  ShenWeiRent
//
//  Created by 李叶夫 on 2019/5/30.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANAlertManager.h"

@implementation ANAlertManager

+ (void)alertSimpleMessage:(NSString *)message handler:(void (^ __nullable)(UIAlertAction *action))handler
{
    [self alertSimpleMessage:message handlerTitle:nil handler:handler];
}

+ (void)alertSimpleMessage:(NSString *)message handlerTitle:(NSString * _Nullable)handlerTitle handler:(void (^ __nullable)(UIAlertAction *action))handler
{
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:handlerTitle ? : @"确认" style:UIAlertActionStyleDefault handler:handler];
    [vc addAction:action];
    [self.currentTopController presentViewController:vc animated:YES completion:nil];
}

+ (void)alertMessage:(NSString *)message
           leftTitle:(NSString *)leftTitle
         leftHandler:(void (^ __nullable)(UIAlertAction *action))leftHandler
          rightTitle:(NSString *)rightTitle
        rightHandler:(void (^ __nullable)(UIAlertAction *action))rightHandler
{
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:leftTitle style:UIAlertActionStyleCancel handler:leftHandler];
    UIAlertAction *rightAction = [UIAlertAction actionWithTitle:rightTitle style:UIAlertActionStyleDefault handler:rightHandler];
    [vc addAction:leftAction];
    [vc addAction:rightAction];
    [self.currentTopController presentViewController:vc animated:YES completion:nil];
}

+ (UIViewController *)currentTopController
{
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([rootVc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabVc = (UITabBarController *)rootVc;
        if ([tabVc.selectedViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *naviVc = tabVc.selectedViewController;
            return naviVc.topViewController;
        }
        else {
            return tabVc.selectedViewController;
        }
    }
    else if ([rootVc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *naviVc = (UINavigationController *)rootVc;
        return naviVc.topViewController;
    }
    else {
        return rootVc;
    }
}

@end
